package com.credithc.customers.manager.controller;

import com.credithc.baseserv.core.codes.CoreCodes;
import com.credithc.baseserv.core.message.Message;
import com.credithc.baseserv.core.message.MessageFactory;
import com.credithc.customers.base.exception.CustomerException;
import com.credithc.customers.manager.service.IFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author zhukai
 * @date 2018/12/20 10:01
 */

@Controller
@RequestMapping("/manager/yzt/v1")
public class FileController {

    private final MessageFactory messageFactory;

    private final IFileService fileService;

    @Autowired
    public FileController(MessageFactory messageFactory, IFileService fileService) {
        this.messageFactory = messageFactory;
        this.fileService = fileService;
    }

    @RequestMapping("/upload")
    @ResponseBody
    public Message upload(@RequestParam MultipartFile file, @RequestParam String type) {
        try {
            fileService.insertFile(type, file);
        } catch (Exception e) {
            if (e instanceof CustomerException) {
                throw new CustomerException(((CustomerException) e).getCode(), ((CustomerException) e).getMsg());
            } else {
                throw new CustomerException(CoreCodes.CORE_SYS_ERROR, "导入异常");
            }
        }
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS);
    }

    @RequestMapping("/upload/get")
    @ResponseBody
    public Message getUpload(@RequestParam(required = false, defaultValue = "1") int page, @RequestParam(required = false, defaultValue = "20") int limit) {
        return messageFactory.getInstance(CoreCodes.CORE_SUCCESS, fileService.queryUpload(page, limit));
    }

    @RequestMapping(value = "/upload/export", method = RequestMethod.GET)
    public void exportExcel(@RequestParam(required = false) Integer importType, @RequestParam(required = false) String exportType, @RequestParam(required = false) String importId)throws IOException {
    }

}
